package com.perfect.controller;

import com.perfect.annotation.RequirePermission;
import com.perfect.base.BaseController;
import com.perfect.base.ResultInfo;
import com.perfect.query.ReasonQuery;
import com.perfect.service.ReasonService;
import com.perfect.vo.Reason;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author YU
 */
@Controller
@RequestMapping("ro")
public class ReasonController extends BaseController {

    @Resource
    private ReasonService reasonService;

    /**
     * 跳转到退货单页面
     * @return
     */
    @RequirePermission(code = "704010")
    @RequestMapping("index")
    public String index(){
        return "reason/reason";
    }

    /**
     * 添加原因类型
     * @param reason
     * @return
     */
    @RequirePermission(code = "704020")
    @RequestMapping("save")
    @ResponseBody
    public ResultInfo save(Reason reason){
        reasonService.saveReason(reason);
        return success("原因类型添加成功");
    }

    /**
     * 更新原因类型
     * @param reason
     * @return
     */
    @RequirePermission(code = "704030")
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo update(Reason reason){
        reasonService.updateReason(reason);
        return success("原因类型更新成功");
    }

    /**
     * 跳转到添加和更新页面
     * @param request
     * @param id
     * @return
     */
    @RequirePermission(code = "704040")
    @RequestMapping("addOrUpdateReasonPage")
    public String addOrUpdateUserPage(HttpServletRequest request, Integer id){
        if (id != null){
            Reason reason = reasonService.selectByPrimaryKey(id);
            request.setAttribute("reason",reason);
        }
        return "reason/add_update";
    }

    /**
     * 展示记录列表
     * @param reasonQuery
     * @return
     */
    @RequirePermission(code = "704050")
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> list(ReasonQuery reasonQuery){
        return reasonService.queryByParamsForTable(reasonQuery);
    }

    /**
     * 删除记录
     * @param ids
     * @return
     */
    @RequirePermission(code = "704060")
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo delete(Integer ids){
        reasonService.deleteReasonByID(ids);
        return success("删除成功");
    }

    /**
     * 多记录删除
     * @param ids
     * @return
     */
    @RequirePermission(code = "704070")
    @RequestMapping("deleteMore")
    @ResponseBody
    public ResultInfo deleteMore(String ids){
        String[] id = ids.split(",");
        for (String i : id){
            delete(Integer.parseInt(i));
        }
        return success("删除成功");
    }
}
